//
//  CihuiView.m
//  Learn
//
//  Created by apple on 2024/4/1.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "CihuiView.h"
#import "CihuiCell.h"
#import "CiHuiHeaderView.h"

@interface CihuiView ()<UICollectionViewDelegate,UICollectionViewDataSource>
/* <#描述#> */

@property (nonatomic, strong) CiHuiHeaderView    * headerView;

@property (nonatomic, strong) UICollectionView    * myCollectionView;
@property (nonatomic, strong) NSMutableArray    * dataArray;

@property (nonatomic, strong) NSMutableArray    * headerArray;
@property (nonatomic, strong) NSMutableArray    * selectArray;

@property (nonatomic, assign) NSInteger            currentPage;       //
@end

@implementation CihuiView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}
- (void)initUI {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.headerView];
    [self addSubview:self.myCollectionView];
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)headerArray{
    if (!_headerArray) {
        _headerArray = [NSMutableArray array];
    }
    return _headerArray;
}
- (NSMutableArray *)selectArray{
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}
- (CiHuiHeaderView *)headerView {
    if (!_headerView) {
        _headerView = [[CiHuiHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, 0.01)];
        WS(WeakSelf)
        _headerView.selectBlock = ^(NSInteger index) {
            if (index == 0) {
                WeakSelf.selectArray = WeakSelf.dataArray.mutableCopy;
            }else {
                NSString *typeName = WeakSelf.headerArray[index - 1][@"versionName"];
                NSPredicate *predicate = [NSPredicate predicateWithFormat:@"versionName=%@",typeName];
                NSArray *results = [WeakSelf.dataArray filteredArrayUsingPredicate:predicate];
                WeakSelf.selectArray = results.mutableCopy;
            }
            [WeakSelf.myCollectionView reloadData];
        };
    }
    return _headerView;
}
- (UICollectionView *)myCollectionView {
    if (!_myCollectionView) {
        UICollectionViewFlowLayout *myCollectionLayout = [[UICollectionViewFlowLayout alloc]init];
        myCollectionLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        myCollectionLayout.minimumInteritemSpacing = 10;
        myCollectionLayout.minimumLineSpacing = 10;
        myCollectionLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
        myCollectionLayout.itemSize = CGSizeMake((SCREEN_W-41)/3.0f,(SCREEN_W-41)/3.0f*144/111 + 5);
        
        _myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, self.headerView.bottom, SCREEN_W, self.height - self.headerView.bottom) collectionViewLayout:myCollectionLayout];
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
        
        _myCollectionView.backgroundColor = UIColor.whiteColor;
        [_myCollectionView registerClass:CihuiCell.class forCellWithReuseIdentifier:@"CihuiCell"];
//        [_myCollectionView registerClass:CiHuiHeaderView.class forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CiHuiHeaderView"];
    }
    return _myCollectionView;
}

- (void)updateList:(NSMutableArray *)list {
    self.dataArray = list.mutableCopy;
    self.selectArray = list.mutableCopy;
    [self.myCollectionView reloadData];
}
- (void)updateHeaderList:(NSMutableArray *)list {
    self.headerArray = list.mutableCopy;
    self.headerView.dataArray = self.headerArray;

    if (self.headerArray.count == 0) {
        self.headerView.height = 0.01;
        self.myCollectionView.top = self.headerView.bottom;
        self.myCollectionView.height = self.height - self.headerView.bottom;
    }
    else {
        CGFloat btnH = 28;
        CGFloat btnSpace= 10;
        NSInteger count = self.headerArray.count ;
        NSInteger row = count/4 + 1;
        CGFloat viewH = 25 + btnSpace*(row - 1) + btnH*row;
        self.headerView.height = viewH;
        self.myCollectionView.top = self.headerView.bottom;
        self.myCollectionView.height = self.height - self.headerView.bottom;
      
    }
}

#pragma mark collectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.selectArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CihuiCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CihuiCell" forIndexPath:indexPath];
    if (self.selectArray.count > indexPath.row) {
        cell.selectID = self.selectID;
        [cell updateItemCellModel:self.selectArray[indexPath.row]];
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CiHuiModel *model = self.selectArray[indexPath.row];
    if (self.CiHuiItemClicked) {
        self.CiHuiItemClicked(model);
    }
}




@end
